package ps.client.plugin.eq2.gui.dialog.curses;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;

import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import ps.client.gui.util.DialogEx;


@SuppressWarnings("serial")
public class PlayerListDialog extends DialogEx {
    
    public static final String DELIMINATOR = "\r\n";
    
    public static final String PREF_NODE_PLAYER_LIST_DIALOG = "PlayerListDialog";
    public static final String PREF_KEY_PLAYERS = "Players";
    public static final String PREF_KEY_RECAST = "Recast";
    
    JPanel contentPanel = new JPanel(new BorderLayout());
    
    PlayerListPanel playerListPanel = new PlayerListPanel(this) {
        @Override
        protected void firePlayerAction(String name) {
            PlayerListDialog.this.firePlayerAction(name);
        }
    };
    
    String[] players = new String[0];
    int[] recasts = new int[0];
    
    
    public PlayerListDialog(Frame owner, String prefNode) {
        super(owner, prefNode + "/PlayerListDialog");
        
        setTitle("Spieler");
        setMinimumSize(new Dimension(200, 300));
        
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        
        contentPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
        contentPanel.add(playerListPanel, BorderLayout.CENTER);
        
        refresh();
        
//        setVisibility();
    };
    
    public void refresh() {
        String val = getPrefNode().get(PREF_KEY_PLAYERS, null);
        if (val == null) {
            players = new String[0];
            recasts = new int[0];
        } else {
            parsePlayersPrefStr();
        }
        
        playerListPanel.removeAllPlayers();
        playerListPanel.addPlayers(players, recasts);
    }
    
    public void loadSettings() {
        super.loadSettings();
        setSize(getPrefNode().getInt(WINDOW_WIDTH, 250),
                getPrefNode().getInt(WINDOW_HEIGHT, 300));
    }
    
    public void savePlayers() {
        getPrefNode().put(PREF_KEY_PLAYERS, createPlayersPrefStr());
        getPrefNode().put(PREF_KEY_RECAST, createPlayersRecastPrefStr());
    }
    
    public String[] getPlayersArray() {
        return players;
    }
    
    public int getRecast(String player) {
        return playerListPanel.playerListModel.getRecastOfPlayer(player);
    }
    
    public String getCompleteRegex() {
        String[] players = getPlayersArray();
        String player = "";
        if (players.length == 1) {
            player = players[0];
        } else if (players.length > 1) {
            player = "(" + players[0];
        }
        for (int i = 1; i < players.length; i++) {
            player += "|" + players[i];
        }
        if (players.length > 1) {
            player += ")";
        }
        return player;
    }
    
    String createPlayersPrefStr() {
        String ret = "";
        if (players != null && players.length > 0) {
            ret += players[0];
            for (int i = 1; i < players.length; i++) {
                ret += DELIMINATOR + players[i];
            }
        }
        
        return ret;
    }
    
    String createPlayersRecastPrefStr() {
        String ret = "";
        if (recasts != null && recasts.length > 0) {
            ret += recasts[0];
            for (int i = 1; i < recasts.length; i++) {
                ret += DELIMINATOR + recasts[i];
            }
        }
        
        return ret;
    }
    
    void parsePlayersPrefStr() {
        String prefValue = getPrefNode().get(PREF_KEY_PLAYERS, "");
        if (prefValue.length() > 0) {
            players = prefValue.split(DELIMINATOR);
            recasts = new int[players.length];
            for (int i = 0; i < recasts.length; i++) {
                recasts[i] = -1;
            }
        } else {
            players = new String[0];
            recasts = new int[0];
        }
    
        prefValue = getPrefNode().get(PREF_KEY_RECAST, "");
        if (prefValue.length() > 0) {
            String[] recastsStr = prefValue.split(DELIMINATOR);
            for (int i = 0; i < recasts.length; i++) {
                recasts[i] = Integer.parseInt(recastsStr[i]);
            }
        }
    }
    
    void updatePlayers() {
        players = playerListPanel.getPlayers();
        recasts = playerListPanel.getRecasts();
        
        savePlayers();
    }
    
    protected void firePlayerAction(String name) {
    }
}
